﻿using ETA.Dtos;
using ETA.Entity;
using ETA.IRepository;
using ETA.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace ETA.Service
{
    public class ReferrerLogService : IReferrerLogService
    {
        #region 属性

        private IReferrerLogRepository _IReferrerLogRepository { set; get; }


        #endregion

        #region 构造函数

        public ReferrerLogService(IReferrerLogRepository IReferrerLogRepository)
        {
            _IReferrerLogRepository = IReferrerLogRepository;
        }

        #endregion


        public QueryReferrerLogResponse QueryReferrerLogList(QueryReferrerLogRequest req, out int count)
        {
            count = 0;
            var response = new QueryReferrerLogResponse();
            try
            {
                var express = QueryReferrerLogExpress(req);
                var list = _IReferrerLogRepository.GetAllList(express);
                count = list.Count;
                response.ReferrerLogs = list.Skip((req.Page - 1) * req.PageSize).Take(req.PageSize).ToList();
                response.Result.IsSuccess = true;

            }
            catch (Exception e)
            {
                response.Result.ErrMessage = e.Message;
                response.Result.IsSuccess = false;
            }
            return response;
        }

        private Expression<Func<ReferrerLog, bool>> QueryReferrerLogExpress(QueryReferrerLogRequest req)
        {
            Expression<Func<ReferrerLog, bool>> express = referrerLog => referrerLog.IsDelete == false;
            if (!string.IsNullOrEmpty(req.UserId))
            {
                express = referrerLog => referrerLog.UserId == req.UserId;
            }
            return express;
        }

        public CreateReferrerLogResponse CreateReferrerLog(CreateReferrerLogRequest req)
        {
            var res = new CreateReferrerLogResponse();
            try
            {
                if (req != null)
                {
                    req.ReferrerLog.ReferrerLogId = Guid.NewGuid().ToString();
                    _IReferrerLogRepository.Merge(req.ReferrerLog);
                    res.Result.IsSuccess = true;
                }
            }
            catch (Exception e)
            {
                res.Result.IsSuccess = false;
                res.Result.ErrMessage = e.Message;
            }
            return res;
        }

        public ModifyReferrerLogResponse ModifyReferrerLog(ModifyReferrerLogRequest req)
        {
            var res = new ModifyReferrerLogResponse();
            try
            {
                if (req != null)
                {
                    req.ReferrerLog.ModifyDate = DateTime.Now;
                    _IReferrerLogRepository.Update(req.ReferrerLog);
                    res.Result.IsSuccess = true;
                }
            }
            catch (Exception e)
            {
                res.Result.IsSuccess = false;
                res.Result.ErrMessage = e.Message;
            }
            return res;
        }


        public QueryReferrerLogSumResponse QueryReferrerLogSumList(QuerySumReferrerLogRequest req, out int count)
        {
            count = 0;
            var response = new QueryReferrerLogSumResponse();
            try
            {
                var list = _IReferrerLogRepository.QueryReferrerLogSumList(req.UserId, req.UserName, req.ReferrerStartDate, req.ReferrerEndDate);
                var res = from l in list
                          group l by new { l.UserId, l.UserName } into g
                          select new ReferrerLogSumDto { UserId= g.Key.UserId,UserName=g.Key.UserName,
                          TotalPoint = g.Sum(p => p.Point).ToString() };
                count = res.Count();
                response.ReferrerLogSumDtos = res.Skip((req.Page - 1) * req.PageSize).Take(req.PageSize).ToList();
                response.Result.IsSuccess = true;

            }
            catch (Exception e)
            {
                response.Result.ErrMessage = e.Message;
                response.Result.IsSuccess = false;
            }
            return response;
        }
    }
}
